﻿using System;
using System.Linq;
using System.Windows.Forms;
using FhlowSharp.Core.FileDef;

namespace FhlowSharp.Gui
{
    public partial class OptionsView : Form
    {
        public FhlowComponent FileStructure
        {
            get;
            private set;
        }

        #region constructor

        public OptionsView()
        {
            InitializeComponent();
        }

        public OptionsView(FhlowComponent component)
        {
            InitializeComponent();
            FileStructure = component;
            DisplayFileStructure(component, FhlowStructView.Nodes);
            ControlBox = true;
            _cancel.Visible = true;
        }


        #endregion constructor

        private void DisplayFileStructure( FhlowComponent component, TreeNodeCollection nodes)
        {
            if(component == null)
                return;
            var newNode = new TreeNode(component.ComponentRegEx);
            nodes.Add(newNode);
            if(component is FhlowComposite)
            {
                foreach (var comp in ((FhlowComposite)component).Children)
                {
                    DisplayFileStructure(comp, newNode.Nodes);
                }
                
            }
        }

        private void AddButton_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(FolderRegexTextBox.Text.Trim()))
            {
                MessageBox.Show("Empty regular expression");
            }
            else if (FhlowStructView.Nodes.Count == 0)
            {
                FhlowStructView.Nodes.Add(FolderRegexTextBox.Text);
            }
            else if (FhlowStructView.SelectedNode == null)
            {
                MessageBox.Show("Please select a node");
            }
            else if (SameLevelNodeExists(FhlowStructView.Nodes, FolderRegexTextBox.Text))
            {
                MessageBox.Show("Node with the same regular expression exists in the same level");
            }
            else
            {
                FhlowStructView.SelectedNode.Nodes.Add(FolderRegexTextBox.Text);
            }
        }

        private bool SameLevelNodeExists(TreeNodeCollection nodes, string match)
        {
            return nodes.Cast<TreeNode>().Any(node => node.Text.Trim() == match.Trim());
        }

        private void SaveButtonClick(object sender, EventArgs e)
        {
            if (FhlowStructView.Nodes.Count == 0) return;

            var root = GenerateList(FhlowStructView.Nodes[0]);
            FileStructure = root;
            Close();
        }

        private FhlowComponent GenerateList(TreeNode root)
        {
            if (root.Nodes.Count == 0)
            {
                return new FhlowLeaf(root.Text);
            }
            var composite = new FhlowComposite(root.Text);

            foreach (TreeNode node in root.Nodes)
            {
                composite.Children.Add(GenerateList(node));
            }
            return composite;
        }

        private void CancelClick(object sender, EventArgs e)
        {
            Close();
        }

        private void _updateButton_Click(object sender, EventArgs e)
        {
            if (FhlowStructView.SelectedNode == null)
            {
                MessageBox.Show("Please select a node");
                return;
            }



        }

        //private bool NodeExists(TreeNodeCollection nodes, string match)
        //{
        //    foreach (TreeNode childNode in nodes)
        //    {
        //        if (NodeExists(childNode, match))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}


        //private bool NodeExists( TreeNode node, string match)
        //{
        //    if (node.Text.Trim() == match.Trim())
        //    {
        //        return true;
        //    }
        //    foreach (TreeNode childNode in node.Nodes)
        //    {
        //        if (NodeExists(childNode, match))
        //        {
        //            return true;
        //        }
        //    }
        //    return false;
        //}
    }
}
